package org.jiuling.aliyun.sms.convert;

import org.jiuling.aliyun.sms.enums.SignTypeEnum;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.model.QuerySmsSignListResponseModel;
import org.jiuling.aliyun.sms.model.enums.AuditStatusEnum;
import org.jiuling.basics.converter.DateConvert;
import org.jiuling.basics.enums.DateTimeFormatEnum;
import org.jiuling.basics.exception.BasicsException;
import org.jiuling.basics.util.EnumUtil;
import org.jiuling.basics.validation.ListVerify;
import org.jiuling.basics.validation.ObjectVerify;
import org.jiuling.basics.validation.StringVerify;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询短信签名列表响应专函
 * @author 紫灵
 * @date 2023年02月20日 15时34分30秒
 */
public class QuerySmsSignListResponseConvert {

    /** 私有构造方法，禁止创建对象 */
    private QuerySmsSignListResponseConvert() {}

    /**
     * 内容站模型
     * @param querySmsSignListResponseBody 获取短信签名列表响应内容
     * @return 获取短信签名列表响应模型
     */
    public static QuerySmsSignListResponseModel bodyToModel(com.aliyun.sdk.service.dysmsapi20170525.models.QuerySmsSignListResponseBody querySmsSignListResponseBody){
        QuerySmsSignListResponseModel querySmsSignListResponseModel = new QuerySmsSignListResponseModel();
        querySmsSignListResponseModel.setCurrentPage(querySmsSignListResponseBody.getCurrentPage());
        querySmsSignListResponseModel.setPageSize(querySmsSignListResponseBody.getPageSize());
        querySmsSignListResponseModel.setTotalCount(querySmsSignListResponseBody.getTotalCount());
        querySmsSignListResponseModel.setCode(querySmsSignListResponseBody.getCode());
        querySmsSignListResponseModel.setMessage(querySmsSignListResponseBody.getMessage());
        querySmsSignListResponseModel.setRequestId(querySmsSignListResponseBody.getRequestId());


        List<com.aliyun.sdk.service.dysmsapi20170525.models.QuerySmsSignListResponseBody.SmsSignList> smsSignBodyList = querySmsSignListResponseBody.getSmsSignList();
        // 哦按段短信签名列表是否为空
        if(!ListVerify.isEmpty(smsSignBodyList)) {
            querySmsSignListResponseModel.setSmsSignList(smsSignBodyList.stream().map(smsSignBody -> {
                QuerySmsSignListResponseModel.SmsSignModel smsSignModel = new QuerySmsSignListResponseModel.SmsSignModel();
                smsSignModel.setOrderId(smsSignBody.getOrderId());
                smsSignModel.setSignName(smsSignBody.getSignName());
                try {
                    String auditStatus = smsSignBody.getAuditStatus();
                    // 判断审批状态是否为空
                    if(!StringVerify.isBlank(auditStatus)) {
                        smsSignModel.setAuditStatus(EnumUtil.valueOfEnum(AuditStatusEnum.class, auditStatus, "name"));
                    }
                    String businessType = smsSignBody.getBusinessType();
                    // 判断签名类型是否为空
                    if(!StringVerify.isBlank(businessType)) {
                        smsSignModel.setBusinessType(EnumUtil.valueOfEnum(SignTypeEnum.class, businessType, "getMsg"));
                    }
                    String createDate = smsSignBody.getCreateDate();
                    // 判断出啊关键时间是否为空
                    if(!StringVerify.isBlank(createDate)) {
                        smsSignModel.setCreateDate(DateConvert.stringToDate(smsSignBody.getCreateDate(), DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                    }
                }catch (BasicsException e) {
                    throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
                }

                com.aliyun.sdk.service.dysmsapi20170525.models.QuerySmsSignListResponseBody.Reason reasonBody = smsSignBody.getReason();
                // 判断审核备注是否为空
                if(!ObjectVerify.isNull(reasonBody)) {
                    QuerySmsSignListResponseModel.SmsSignModel.ReasonModel reasonModel = new QuerySmsSignListResponseModel.SmsSignModel.ReasonModel();
                    reasonModel.setRejectInfo(reasonBody.getRejectInfo());
                    reasonModel.setRejectSubInfo(reasonBody.getRejectSubInfo());

                    String rejectDate = reasonBody.getRejectDate();
                    // 判断审批未通过的时间是否为空
                    if (!StringVerify.isBlank(rejectDate)) {
                        try {
                            reasonModel.setRejectDate(DateConvert.stringToDate(rejectDate, DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                        } catch (BasicsException e) {
                            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
                        }
                    }

                    smsSignModel.setReason(reasonModel);
                }
                return smsSignModel;
            }).collect(Collectors.toList()));
        }
        return querySmsSignListResponseModel;
    }

    /**
     * 内容站模型
     * @param querySmsSignListResponseBody 获取短信签名列表响应内容
     * @return 获取短信签名列表响应模型
     */
    public static QuerySmsSignListResponseModel bodyToModel(com.aliyun.dysmsapi20170525.models.QuerySmsSignListResponseBody querySmsSignListResponseBody){
        QuerySmsSignListResponseModel querySmsSignListResponseModel = new QuerySmsSignListResponseModel();
        querySmsSignListResponseModel.setCurrentPage(querySmsSignListResponseBody.getCurrentPage());
        querySmsSignListResponseModel.setPageSize(querySmsSignListResponseBody.getPageSize());
        querySmsSignListResponseModel.setTotalCount(querySmsSignListResponseBody.getTotalCount());
        querySmsSignListResponseModel.setCode(querySmsSignListResponseBody.getCode());
        querySmsSignListResponseModel.setMessage(querySmsSignListResponseBody.getMessage());
        querySmsSignListResponseModel.setRequestId(querySmsSignListResponseBody.getRequestId());


        List<com.aliyun.dysmsapi20170525.models.QuerySmsSignListResponseBody.QuerySmsSignListResponseBodySmsSignList> smsSignBodyList = querySmsSignListResponseBody.getSmsSignList();
        // 哦按段短信签名列表是否为空
        if(!ListVerify.isEmpty(smsSignBodyList)) {
            querySmsSignListResponseModel.setSmsSignList(smsSignBodyList.stream().map(smsSignBody -> {
                QuerySmsSignListResponseModel.SmsSignModel smsSignModel = new QuerySmsSignListResponseModel.SmsSignModel();
                smsSignModel.setOrderId(smsSignBody.getOrderId());
                smsSignModel.setSignName(smsSignBody.getSignName());
                try {
                    String auditStatus = smsSignBody.getAuditStatus();
                    // 判断审批状态是否为空
                    if(!StringVerify.isBlank(auditStatus)) {
                        smsSignModel.setAuditStatus(EnumUtil.valueOfEnum(AuditStatusEnum.class, auditStatus, "name"));
                    }
                    String businessType = smsSignBody.getBusinessType();
                    // 判断签名类型是否为空
                    if(!StringVerify.isBlank(businessType)) {
                        smsSignModel.setBusinessType(EnumUtil.valueOfEnum(SignTypeEnum.class, businessType, "getMsg"));
                    }
                    String createDate = smsSignBody.getCreateDate();
                    // 判断出啊关键时间是否为空
                    if(!StringVerify.isBlank(createDate)) {
                        smsSignModel.setCreateDate(DateConvert.stringToDate(smsSignBody.getCreateDate(), DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                    }
                } catch (BasicsException e) {
                    throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
                }

                com.aliyun.dysmsapi20170525.models.QuerySmsSignListResponseBody.QuerySmsSignListResponseBodySmsSignListReason reasonBody = smsSignBody.getReason();
                // 判断审核备注是否为空
                if(!ObjectVerify.isNull(reasonBody)) {
                    QuerySmsSignListResponseModel.SmsSignModel.ReasonModel reasonModel = new QuerySmsSignListResponseModel.SmsSignModel.ReasonModel();
                    reasonModel.setRejectInfo(reasonBody.getRejectInfo());
                    reasonModel.setRejectSubInfo(reasonBody.getRejectSubInfo());

                    String rejectDate = reasonBody.getRejectDate();
                    // 判断审批未通过的时间是否为空
                    if (!StringVerify.isBlank(rejectDate)) {
                        try {
                            reasonModel.setRejectDate(DateConvert.stringToDate(rejectDate, DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                        }catch (BasicsException e) {
                            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
                        }
                    }

                    smsSignModel.setReason(reasonModel);
                }
                return smsSignModel;
            }).collect(Collectors.toList()));
        }
        return querySmsSignListResponseModel;
    }

}
